package com.xjh.t.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.WebUtils;
import com.xjh.t.domain.*;
import com.xjh.t.domain.OutWords;
import com.xjh.t.service.IOutWordsService;
import com.xjh.t.service.IOutWordsService;
import com.xjh.t.vo.OutWordsVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.json.JSONParser;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/reciteWord")
public class OutWordsController {


    @Autowired
    private IOutWordsService outWordsService;

    @RequestMapping("loadAllOutWords")
    public DataGridView loadAllOutWords(OutWordsVo vo) {

        IPage<OutWords> page = new Page<>(vo.getPage(), vo.getLimit());
        QueryWrapper<OutWords> queryWrapper = new QueryWrapper<>();
        ClientUser user =  (ClientUser) WebUtils.getSession().getAttribute("user");

        queryWrapper.eq("belong_level",vo.getBelongLevel());
        queryWrapper.eq("user_id",user.getId());

        queryWrapper.ge(vo.getStartTime() != null, "create_time", vo.getStartTime());
        queryWrapper.le(vo.getEndTime() != null, "create_time", vo.getEndTime());

        queryWrapper.orderByDesc("create_time");

        this.outWordsService.page(page, queryWrapper);

        return new DataGridView(page.getTotal(), page.getRecords());

    }

    @RequestMapping("addEnglishWord")
    public ResultObj addEnglishWord(String createtime,int belongLevel,String english,String chinese,String sentence) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ClientUser user =  (ClientUser) WebUtils.getSession().getAttribute("user");

        QueryWrapper<OutWords> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(createtime != null, "create_time", sdf.parse(createtime));
        queryWrapper.eq( "belong_level", belongLevel);
        queryWrapper.eq("user_id",user.getId());

        List<OutWords> outWordsList = outWordsService.list(queryWrapper);
        OutWords outWordsTemp;
        if(outWordsList.size() > 0)
        {
            outWordsTemp = outWordsList.get(0);
            JSONArray arr = new JSONArray(outWordsTemp.getWordList());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("word",english);
            jsonObject.put("chinese",chinese);
            jsonObject.put("sentence",sentence);
            arr.put(jsonObject);
            outWordsTemp.setWordList(arr.toString());
            this.outWordsService.updateById(outWordsTemp);
        }
        else {

            outWordsTemp = new OutWords();

            outWordsTemp.setUserId(user.getId());
            outWordsTemp.setBelongLevel(belongLevel);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String datestr = simpleDateFormat.format(new Date());
            outWordsTemp.setCreateTime(sdf.parse(datestr + " 00:00:00"));

            List<JSONObject> arr = new ArrayList<>();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("word",english);
            jsonObject.put("chinese",chinese);
            jsonObject.put("sentence",sentence);
            arr.add(jsonObject);
            outWordsTemp.setWordList(arr.toString());
            this.outWordsService.save(outWordsTemp);
        }

        //
        return ResultObj.ADD_SUCESS;
    }

    @RequestMapping("deleteEnglishWord")
    public ResultObj deleteEnglishWord(int id,String englishword) throws ParseException {

        OutWords outWords = this.outWordsService.getById(id);

        if(outWords != null) {

            JSONArray arr = new JSONArray(outWords.getWordList());
            int index=0;
            for(Object jsonObject:arr)
            {
                JSONObject jsonObject1 = (JSONObject)jsonObject;
                if(((JSONObject) jsonObject).get("word").equals(englishword))
                {
                    arr.remove(index);
                    break;
                }
                else index++;
            }
            outWords.setWordList(arr.toString());
            this.outWordsService.updateById(outWords);
            return ResultObj.DELETE_SUCESS;
        }
        else
            return ResultObj.DELETE_ERROR;
    }
}

